<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 */
namespace app\api\controller;

use think\Db;

class Article extends Base
{
    public function info()
    {
        $article_id = I('article_id/d', 0);
        $article = Db::name('article')->where("article_id", $article_id)->find();
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '文章不存在']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * mike
     * 2018-6-18
     * 文章的灵感8下的分类
     */
    public function catList()
    {
        $article_id = I('cat_id/d', 8);
        $article = Db::name('article_cat')->where("parent_id", $article_id)->field('cat_id as id,cat_name as title ,cat_desc')->select();
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '分类下没有内容']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * mike
     * 2018-08-07
     * 图片直接转换成对应的实际地址
     * 摩卡婚礼文章分类，类别+文章
     */
    public function cat_art_list()
    {
        $article_id = I('cat_id/d', 8);
        $article = Db::name('article_cat')->where("parent_id", $article_id)->field('cat_id as id,cat_name as title ,cat_desc')->select();
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '分类下没有内容']);
        }

        $domain = C('domain_url');

        foreach ($article as $key=>$value){
            $map['cat_id'] = $value['id'];
            $article[$key]['artList']  = Db::name('article')->alias('a')->where($map)->field("a.title,a.description,a.click,concat('$domain',a.thumb) as thumb,a.zan as zanNum,article_id")->select();
        }

        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    public function marriedCatList()
    {
        $article_id = I('cat_id/d', 8);

        $ArticleCat = new \app\admin\logic\MarriedArticleCatLogic();
        $cats = $ArticleCat->article_cat_list(1,0,false);
        if (!$cats) {
            $this->ajaxReturn(['status' => -1, 'msg' => '分类下没有内容']);
        }


        foreach ($cats as $key=>$val){
            $val['count'] = Db::name('married_article')->where(array('cat_id'=>$val['cat_id'],'is_open'=>1))->count();
            $data[] = $val;
        }
        $total_count = 0;
        foreach ($data as $key=>$val){
           $total_count += $val['count'];
        }
        $data[0]['count'] = $total_count;
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $data]);
    }

    /**
     * mike
     * 2018-6-18
     * 文章列表
     */
    public function artList()
    {
        $cat_id = I('cat_id/d', 9);
        $type = I('type/d', 'no');
        if($type == 'yes' || $type == 'yes_no_zan'){
            if (!$this->user_id && $type != 'yes_no_zan') {
                $this->ajaxReturn(['status'=>-100, 'msg'=>'还未登陆', 'result'=>'']);
            }
            $map['cat_id'] = $cat_id;
            $article = Db::name('article')->alias('a')->where($map)->field('a.title,a.description,a.click,a.thumb,a.zan as zanNum,article_id')->order('sort desc')->select();

            if($type == 'yes'){
                $maps['user_id'] = $this->user_id;
                foreach ($article as $key=>$value){

                   $maps['article_id'] = $value['article_id'];
                   $list = Db::name('article_zan')->where($maps)->find();
                   if(!$list){
                       $article[$key]['zan'] = 1;
                   }else{
                       $article[$key]['zan'] = 0;
                   }
                }
            }


        }else{
            $article = Db::name('article')->where("cat_id", $cat_id)->order('sort desc')->select();
        }
        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '分类下没有内容']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }

    /**
     * mike
     * 2018-6-18
     * 点赞功能
     */
    public function artZan(){
        if (!$this->user_id) {
            $this->ajaxReturn(['status'=>-1, 'msg'=>'还未登陆', 'result'=>'']);
        }
        $article_id = I('article_id/d', 8);
        $list = Db::name('article_zan')->where(array('article_id'=>$article_id,'user_id'=>$this->user_id))->find();
        if (!$list) {
            $data['user_id'] = $this->user_id;
            $data['article_id'] = $article_id;
            Db::name('article_zan')->add($data);
            Db::name('article')->where(array('article_id'=>$article_id))->setInc('zan');
            $this->ajaxReturn(['status' => 1, 'msg' => '添加成功','result'=>'in']);
        }else{
            Db::name('article_zan')->where(array('article_id'=>$article_id,'user_id'=>$this->user_id))->delete();
            Db::name('article')->where(array('article_id'=>$article_id))->setDec('zan');
            $this->ajaxReturn(['status' => 1, 'msg' => '取消成功','result'=>'out']);
        }
    }

    /**
     * mike
     * 2018-7-18
     * 文章列表
     */
    public function marriedArtList()
    {
        $cat_id = I('cat_id/d', 9);
        $type = I('type','no');


        $catIds = Db::name('married_article_cat')->where('parent_id',$cat_id)->getField('cat_id',true);

        if($type == 'no' && $catIds==false) {
            $article = Db::name('married_article')->where(array('cat_id'=>$cat_id,'is_open'=>1))->order('sort_order desc')->select();
        }else{
            if(!$catIds){

                $cat_list = Db::name('married_article_cat')->where('parent_id',$cat_id)->getField('cat_id',true);
            }else{
                $cat_list = $catIds;
            }

            $article = Db::name('married_article')->where(array('cat_id'=>array('in',$cat_list),'is_open'=>1))->order('sort_order desc')->select();
        }

        if (!$article) {
            $this->ajaxReturn(['status' => -1, 'msg' => '分类下没有内容']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $article]);
    }



}